﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net;
using System.IO;

namespace OAuth
{
    public static class OAuthCommon
    {
        /// <summary>
        /// 获取的当前时间戳整型值
        /// </summary>
        /// <returns>时间戳整型值</returns>
        public static string GetOAuthTimeStamp()
        {
            TimeSpan ts = DateTime.UtcNow - new DateTime(1970, 1, 1, 0, 0, 0, 0);
            return Convert.ToInt64(ts.TotalSeconds).ToString();
        }

        /// <summary>
        /// 得到随机值
        /// </summary>
        /// <returns>GUID</returns>
        public static string GetOAuthNonce()
        {
            return Guid.NewGuid().ToString().Substring(0, 32);
        }

        /// <summary>
        /// 对字符串进行编码
        /// </summary>
        /// <param name="value">要编码的字符串</param>
        /// <returns>编码后的字符串</returns>
        public static string UrlEncode(string value)
        {
            StringBuilder result = new StringBuilder();
            string unreservedChars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789-_.~";
            foreach (char symbol in value)
            {
                if (unreservedChars.IndexOf(symbol) != -1)
                {
                    result.Append(symbol);
                }
                else
                {
                    result.Append('%' + String.Format("{0:X2}", (int)symbol));
                }
            }
            return result.ToString();
        }

        /// <summary>
        /// 发送http请求
        /// </summary>
        /// <param name="url">请求地址.包括参数</param>
        /// <param name="OAuthType">认证方式</param>
        /// <param name="RequestType">请求方式</param>
        /// <returns></returns>
        public static string HttpRequest(Uri url,OAuthEnum.OAuthType OAuthType,OAuthEnum.RequestType RequestType)
        {
            HttpWebRequest webRequest;

            string pathUrl = url.AbsolutePath;
            List<OAuthParameter> urlPar = new List<OAuthParameter>();
            string[] _par = url.Query.Split("&".ToCharArray());
            foreach (string par in _par)
            {
                string[] NameVal = par.Split("=".ToCharArray());
                OAuthParameter OnePar = new OAuthParameter(NameVal[0], NameVal[1]);
                urlPar.Add(OnePar);
            }
            urlPar.Sort(new OAuthParameterCompre());

            if (RequestType == OAuthEnum.RequestType.GET)
            {
                if (OAuthType == OAuthEnum.OAuthType.QueryString)
                {
                    webRequest = WebRequest.Create(url) as HttpWebRequest;
                    webRequest.Method = "GET";
                    webRequest.ServicePoint.Expect100Continue = false;
                    webRequest.Timeout = 20000;
                }
                else
                { 

                }
            }
            else
            {
                webRequest = WebRequest.Create(url.AbsolutePath) as HttpWebRequest;
                webRequest.Method = "POST";
                webRequest.ContentType = "application/x-www-form-urlencoded";
                webRequest.ServicePoint.Expect100Continue = false;
                webRequest.Timeout = 20000;

                StreamWriter requestWriter = new StreamWriter(webRequest.GetRequestStream());
                requestWriter.Write(url.Query);
                requestWriter.Close();
                requestWriter = null;
            }
            StreamReader responseReader = new StreamReader(webRequest.GetResponse().GetResponseStream());
            string responseData = responseReader.ReadToEnd();
            return string.Empty;
        }
    }
}
